package com.wxsoft.java.thread.v9;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 
 * @author chenliang
 *
 */
public class Test {
	// public volatile int inc = 0;
	public int inc = 0;
	Lock lock = new ReentrantLock();

	// public synchronized void increase() {
	// synchronized (Test.class) {
	//
	// inc++;//inc = inc+1 ; 读取、 加、 赋值
	// }
	// }
	public void increase() {
		lock.lock();
		try{
			inc++;// inc = inc+1 ; 读取、 加、 赋值
		}catch(Exception e){
			
		}finally{
			lock.unlock();
		}
	}

	public static void main(String[] args) {
		final Test test = new Test();
		for (int i = 0; i < 10; i++) {
			new Thread() {
				public void run() {
					for (int j = 0; j < 1000; j++)
						test.increase();
				};
			}.start();
		}

		while (Thread.activeCount() > 1)
			// 保证前面的线程都执行完
			Thread.yield();
		System.out.println(test.inc);
	}
}